package com.bookstore.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.bookstore.model.Order;
import com.bookstore.utils.DBConnection;

public class OrderDAOImpl implements OrderDAO {

	private static final String SAVE = "insert into d_order (user_id,status,order_time,order_desc,total_price,receive_name,full_address,postal_code,mobile,phone) values (?,?,?,?,?,?,?,?,?,?)";

	@Override
	public void save(Order order) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = DBConnection.getConnection();
			statement = connection.prepareStatement(SAVE, Statement.RETURN_GENERATED_KEYS);
			statement.setInt(1, order.getUserId());
			statement.setInt(2, order.getStatus());
			statement.setLong(3, order.getOrderTime());
			statement.setString(4, order.getOrderDesc());
			statement.setDouble(5, order.getTotalPrice());
			statement.setString(6, order.getReceiveName());
			statement.setString(7, order.getFullAddress());
			statement.setString(8, order.getPostalCode());
			statement.setString(9, order.getMobile());
			statement.setString(10, order.getPhone());
			statement.executeUpdate();
			resultSet = statement.getGeneratedKeys();
			resultSet.next();
			int id = resultSet.getInt(1);
			order.setId(id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(resultSet, statement, connection);
		}
	}

}
